<?php


namespace Trawind\Basics\Exceptions;


use Exception;
use Illuminate\Http\Request;
use Trawind\Helpers\ResponseHelper;
use Trawind\Basics\Enums\BusinessCode;
use Symfony\Component\HttpFoundation\Response;

abstract class TrawindException extends Exception
{

    protected $code = BusinessCode::SYSTEM_ERROR;

    protected $statusCode = Response::HTTP_NOT_IMPLEMENTED;

    protected $options = [];

    public function __construct(string $message, ?int $code = null, $previous = null)
    {
        parent::__construct($message, $code ?? $this->code, $previous);
    }


    public function setStatusCode(int $statusCode)
    {
        $this->statusCode = $statusCode;
        return $this;
    }


    public function getStatusCode()
    {
        return $this->statusCode;
    }


    public function setOptions(array $options)
    {
        $this->options = $options;
        return $this;
    }


    public function getOptions()
    {
        return $this->options;
    }


    public static function error(string $message)
    {
        throw new static($message);
    }


    public function render(Request $request)
    {
        return ResponseHelper::exception(
            $this->getCode(),
            $this->getMessage(),
            $this->getStatusCode(),
            $this->getOptions()
        );
    }

}